//
//  CNBBSListViewModel.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2018/5/29.
//  Copyright © 2018年 CSDN. All rights reserved.
//

#import "CNBBSListViewModel.h"
#import <CNBaseTool/CNCategoryHeader.h>

@interface CNBBSListViewModel()

@property (nonatomic,strong) CNBBSListModel *model;

@end

@implementation CNBBSListViewModel
/**
 *  获取有多少个
 *
 *  @return 返回个数
 */
-(NSInteger)rowNumber
{
    return self.dataArr.count;
}

/**
 *  判断是否是最大页数
 *
 *
 */
- (BOOL)isHasMore{
    return self.model.data.count > 0;
}

#pragma mark - 数据请求

-(void)refreshBBSListDataWithTocipType:(NSString *)tocipType CompletionHandle:(CompletionHandle)completionHandle
{
    self.page = 1;
    return [self getBBSListDataFromNetWithTocipType:tocipType CompleteHandle:completionHandle];
}

- (void)getMoreBBSListDataWithTocipType:(NSString *)tocipType CompletionHandle:(CompletionHandle)completionHandle
{
    //如果当前页数已经是最大页数，那么没有必要再发送获取更多请求了，这样会浪费用户流量
//    if (self.isHasMore) {
        self.page += 1;
        [self getBBSListDataFromNetWithTocipType:tocipType CompleteHandle:completionHandle];
//    }else{
//        NSError *err=[NSError errorWithDomain:@"" code:999 userInfo:@{NSLocalizedDescriptionKey:@"没有更多数据了"}];
//        completionHandle(err);
//    }
    
}

- (void)getBBSListDataFromNetWithTocipType:(NSString *)tocipType CompleteHandle:(CompletionHandle)completionHandle{
    self.dataTask = [CNBBSNetManager getBBSHotTopicListWithTocipType:tocipType AndPage:self.page completionHandle:^(CNBBSListModel *model, NSError *error) {
    
        if (!error) {
            self.code = model.code;
            self.message = model.msg;
            
            if (self.code == 200) {
                self.model = model;
                if (self.page == 1) {
                    [self.dataArr removeAllObjects];
                }
                [self.dataArr addObjectsFromArray:model.data];
            }
            else
            {
                //为了防止token失效时页数加1导致重新添加数据时中间少页
                if (self.page > 1)
                {
                    self.page -= 1;
                }
            }
        }
        completionHandle(error);
    }];
}

/**
 *  获取CNSearchTagesDataDataModel
 *
 *  @param row 根据行
 *
 *  @return 返回CNSearchTagesDataDataModel
 */

-(CNBBSListDataModel *)bbsListDataModelForRowInBBSList:(NSInteger)row{
    if (row == 0) {
        return self.dataArr.firstObject;
    }
    return self.dataArr[row];
}

/**  */
-(BOOL)containImageForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].contain_image;
}

/**  */
-(NSString *)parentForumForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].parent_forum;
}

/**  */
-(NSString *)postIdForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].post_id;
}

/**  */
-(NSInteger)statusForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].status;
}

/**标题  */
-(NSString *)titleForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].title;
}

/**  */
-(NSString *)forumForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].forum;
}

/**  */
-(BOOL)isExpiredForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].is_expired;
}

/**  */
-(NSInteger)floorForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].floor;
}

/**昵称  */
-(NSString *)nicknameForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].nickname;
}

/**  */
-(NSString *)forumUrlnameForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].forum_urlname;
}

/**分数  */
-(NSString *)scoreForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].score;
}

/**  */
-(BOOL)isDiggedForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].is_digged;
}

/**  */
-(BOOL)isBuryedForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].is_buryed;
}

/**是否顶  */
-(BOOL)isTopForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].is_top;
}

/**  */
-(NSString *)IdForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].ID;
}

/**  */
-(NSString *)parentForumUrlnameForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].parent_forum_urlname;
}

/**是否精  */
-(BOOL)isRecommendForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].is_recommend;
}

/**回复数  */
-(NSString *)postCountForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].post_count;
}

/**  */
-(NSString *)lastPostCreatedAtForRow:(NSInteger)row
{
    return [CSDNTool compareCurrentTime:[self bbsListDataModelForRowInBBSList:row].last_post_created_at];
}

/**  */
-(NSString *)viewCountForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].view_count;
}

/**摘要  */
-(NSString *)introductionForRow:(NSInteger)row
{
    return [NSString stringWithFormat:@"%@...",[self bbsListDataModelForRowInBBSList:row].introduction];
}

/**时间  */
-(NSString *)createdAtForRow:(NSInteger)row
{
    return [CSDNTool compareCurrentTime:[self bbsListDataModelForRowInBBSList:row].created_at];
}

/**用户名  */
-(NSString *)usernameForRow:(NSInteger)row
{
    return [self bbsListDataModelForRowInBBSList:row].username;
}

/**头像  */
-(NSURL *)avatarForRow:(NSInteger)row
{
    if ([[self bbsListDataModelForRowInBBSList:row].avatar hasPrefix:@"//"])
    {
        return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",@"https:",[self bbsListDataModelForRowInBBSList:row].avatar]];
    }
    else
    {
        return [NSURL URLWithString:[self bbsListDataModelForRowInBBSList:row].avatar];
    }
}

@end
